package Thread;

class MyBlockingQueue {
    private String[] data = null;
    private static int head = 0;
    private static int tail = 0;
    private static int size = 0;

    public MyBlockingQueue(int capacity) {
        data = new String[capacity];
    }

    //入队列
    public void put(String s){
        synchronized (this) {
            if (size == data.length) {
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            data[tail] = s;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;

            this.notify();
        }
    }

    //出队列
    public String take(){
        String ret = "";
        synchronized (this){
            if (size == 0){
                try {
                    this.wait();
                } catch (InterruptedException e) {

                }
            }
            ret = data[head];
            head++;
            if(head >= data.length){
                head = 0;
            }
            size--;

            this.notify();
        }
        return ret;
    }

}

public class Demo35 {
    public static void main(String[] args) {

    }
}
